//
//  KFWMessageViewController.m
//  KWNLaywerClient
//
//  Created by 孔六五 on 15/1/12.
//  Copyright (c) 2015年 张明磊. All rights reserved.
//

#import "KFWMessageViewController.h"
#import "KFWMessageTabCell.h"
#import "KFWMessageCommentCell.h"
#import "KFWMessageInputView.h"
#import "KFLQuestionDetailRequest.h"
#import "Model_message.h"
#import "Model_QuestionComments.h"
#import "Model_questionDetailNew.h"
#import "KWLLoginViewController.h"
#import "UIImageView+WebCache.h"
#import "TQStarRatingView.h"
#import "KWNPhotoDetailViewController.h"
#import "KWLLayerViewController.h"
#import "KFLLaywerTalkRequest.h"
#import "KWNFeedbackViewController.h"
#import "KWNNetwork.h"
#import "MJRefresh.h"
#import "KFLUpdataUnreadNumRequest.h"

@interface KFWMessageViewController ()<UITableViewDataSource,UITableViewDelegate,KFWMessageTabCellDelegate,MWPhotoBrowserDelegate,KFWMessageInputViewDelegate,UIActionSheetDelegate>
/**
 *  问题ID
 */
@property (strong, nonatomic) NSString *string_id;
@property BOOL isUpdataUnread;                    //是否需要更新未读数
@property (strong, nonatomic) UIView *view_title;
@property (strong, nonatomic) UITableView *tab_messageTab;
@property (strong, nonatomic) NSArray *array_location;
@property (strong, nonatomic) NSMutableArray *array_photo;

@property (strong, nonatomic) NSMutableArray *arr_messageData;
@property BOOL bool_messageFirst;

@property (strong, nonatomic) KFWMessageInputView *inputView;

@end

@implementation KFWMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = RGB(239, 239, 244);
    self.view.backgroundColor = [UIColor whiteColor];
    _bool_messageFirst = YES;
    _arr_messageData = [[NSMutableArray alloc] init];
    _array_photo = [[NSMutableArray alloc] init];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSArray *array_tmp = @[
                               @"北京",@"上海",@"天津",@"重庆",@"黑龙江省",@"吉林省",@"辽宁省",@"河北省",@"山西省",@"内蒙古",@"山东省",@"安徽省",@"江苏省",@"浙江省",@"江西省",@"福建省",@"河南省",@"湖北省",@"湖南省",@"广东省",@"广西",@"海南省",@"四川省",@"贵州省",@"云南省",@"西藏",@"陕西省",@"甘肃省",@"青海省",@"宁夏",@"新疆",@"香港",@"澳门",@"台湾省"];
        dispatch_async(dispatch_get_main_queue(), ^{
            _array_location = array_tmp;
        });
    });
    
    [self UI_CreatMessageTab];
    [self UI_MessageQuestionTitle];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (id)initWithQID:(NSString *)string_id andUpdataUnread:(BOOL)isupdataunread
{
    self = [super init];
    if (self) {
        self.string_id = string_id;
        self.isUpdataUnread = isupdataunread;
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if(_bool_messageFirst == YES)
    {
        [self getMessageData];
    }
}

- (void)UI_MessageQuestionTitle
{
    _view_title = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44 + IOS7_SIZE)];
    _view_title.backgroundColor = RGB(91, 165, 229);
    [self.view addSubview:_view_title];
    
    UIButton *button_back = [UIButton buttonWithType:UIButtonTypeCustom];
    button_back.frame = CGRectMake(0, IOS7_SIZE, 60, 45);
    [button_back setImage:[UIImage imageFileName:@"button_back.png"] forState:UIControlStateNormal];
    [button_back addTarget:self action:@selector(didClickButton_back:) forControlEvents:UIControlEventTouchUpInside];
    [_view_title addSubview:button_back];
    
    UILabel *label_title = [[UILabel alloc] initWithFrame:CGRectMake(_view_title.frame.origin.x,IOS7_SIZE + 5,_view_title.frame.size.width,35)];
    label_title.backgroundColor = [UIColor clearColor];
    label_title.textColor = ColorOfTitle;
    label_title.textAlignment = NSTextAlignmentCenter;
    label_title.font = FontOfHelveticaBold(16);
    [_view_title addSubview:label_title];
    label_title.text = @"问题详情";
    
    UIButton *button_edit = [UIButton buttonWithType:UIButtonTypeCustom];
    button_edit.frame = CGRectMake(250, IOS7_SIZE, 60, 45);
    [button_edit setTitle:@"举报" forState:UIControlStateNormal];
    [button_edit setTitleColor:ColorOfTitle forState:UIControlStateNormal];
    button_edit.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
    [button_edit addTarget:self action:@selector(myDetailReportBtn_click:) forControlEvents:UIControlEventTouchUpInside];
    [_view_title addSubview:button_edit];
}

- (void)UI_CreatMessageTab
{
    _tab_messageTab = [[UITableView alloc] initWithFrame:CGRectMake(0, 44 + IOS7_SIZE - 20,MainScreenWidth , ViewHeight - _view_title.frame.origin.y - _view_title.frame.size.height) style:UITableViewStylePlain];
    if (IOS6_VERSION) {
        _tab_messageTab.frame = CGRectMake(0, 44,MainScreenWidth , ViewHeight - 44);
    }
    else
    {
        _tab_messageTab.frame = CGRectMake(0, 44 + IOS7_SIZE - 20,MainScreenWidth , ViewHeight - 44 + 20 - IOS7_SIZE);
    }
    _tab_messageTab.delegate = self;
    _tab_messageTab.dataSource = self;
    _tab_messageTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    __weak KFWMessageViewController *aself = (KFWMessageViewController *)self;
    //加入上拉加载更多
    [_tab_messageTab addFooterWithCallback:^{
//        [aself getMyListData];
        [aself getMessageData];
    }];

    [self.view addSubview:_tab_messageTab];
    
}

//返回
- (void)didClickButton_back:(id)sender
{
    [self popViewController];
}

//举报
- (void)myDetailReportBtn_click:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:nil
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:nil
                                  otherButtonTitles:@"垃圾信息或无意义内容",@"广告或营销内容",@"违反法律法规的内容",@"不宜公开讨论的政治内容", @"手动填写举报内容",nil];
    actionSheet.tag = 100;
    [actionSheet showInView:self.view];

}

//数据
- (void)getMessageData
{
    [SVProgressHUD showWithStatus:@"加载中..." maskType:SVProgressHUDMaskTypeClear];

    KFLQuestionDetailRequest *request = [[KFLQuestionDetailRequest alloc] initRequest];
//    request.questionDetail_qId = @"344";
    request.questionDetail_qId = _string_id;
    __weak KFWMessageViewController *aself = self;
    
    [request startToRequestWithBlock:^(NSInteger stateCode, NSMutableArray *result) {
        
        [_tab_messageTab footerEndRefreshing];
        switch (stateCode) {
            case 10000:
            {
                [SVProgressHUD dismiss];

                [aself.arr_messageData removeAllObjects];
                [aself.arr_messageData addObjectsFromArray:result];
                
                [aself.tab_messageTab reloadData];
                
                if (aself.inputView == nil)
                {
                    aself.inputView = [KFWMessageInputView creatKFWMessageInputViewWithFrame:aself.view.bounds delegate:aself];
                }
//                if (aself.inputView == nil)
//                {
//                    if (aself.arr_messageData.count>0)
//                    {
//                        Model_questionDetailNew *qD = [aself.arr_messageData objectAtIndex:0];
//                        if ([qD.string_islawyansw integerValue] != 1 && qD.model_Comments == nil)
//                        {
//                            //可以回答的问题
//                            aself.inputView = [KFWMessageInputView creatKFWMessageInputViewWithFrame:aself.view.bounds delegate:aself];
//                        }
//                    }
//                }

                Model_questionDetailNew *qD = [_arr_messageData objectAtIndex:0];
                NSIndexPath *ip = nil;
                
                if (aself.bool_messageFirst == YES) {
                    aself.bool_messageFirst = NO;
                    [aself updataUnreadNum];
                }
                
                if (qD.model_Comments) {
                    ip = [NSIndexPath indexPathForRow:aself.arr_messageData.count inSection:0];
                }
                else
                {
                    ip = [NSIndexPath indexPathForRow:aself.arr_messageData.count - 1 inSection:0];

                }
                [aself.tab_messageTab scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:NO];
            }
                break;
            case 10003:
            {
                [SVProgressHUD dismiss];
                [aself messageTokenIsFailure];
            }
                break;
            default:
            {
                NSString *string_message = result[0];
                [SVProgressHUD dismissWithError:string_message afterDelay:1.5];
            }
                break;
        }
        
        
        if (aself.inputView == nil) {
            aself.inputView = [KFWMessageInputView creatKFWMessageInputViewWithFrame:aself.view.bounds delegate:aself];
        }

//        if (aself.inputView == nil) {
//            if (aself.arr_messageData.count>0) {
//                Model_questionDetailNew *qD = [aself.arr_messageData objectAtIndex:0];
//                if ([qD.string_islawyansw integerValue] != 1 && qD.model_Comments == nil) {
//                    //可以回答的问题
//                    aself.inputView = [KFWMessageInputView creatKFWMessageInputViewWithFrame:aself.view.bounds delegate:aself];
//                }
//            }
//        }
    }];
    
}

#pragma mark - 更新未读数 -
- (void)updataUnreadNum
{
    KFLUpdataUnreadNumRequest *request = [[KFLUpdataUnreadNumRequest alloc] initRequest];
    request.question_id = self.string_id;
    request.unread = @"0";
    [request startToRequestWithBlock:^(NSInteger stateCode, NSMutableArray *result) {
        switch (stateCode) {
            case 10000:
            {
            }
                break;
            default:
                break;
        }
    }];
}

#pragma mark - UIActionSheetDelegate -

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    //NSLog(@"%li", (long)buttonIndex);
    
    NSString *buttontitle = [actionSheet buttonTitleAtIndex:buttonIndex];
    if (buttonIndex == actionSheet.cancelButtonIndex) {
        return;
    }
    if (actionSheet.tag == 100)
    {
        if (buttonIndex != 4)
        {
            [SVProgressHUD showWithStatus:@"加载中,请稍候..." maskType:SVProgressHUDMaskTypeClear];
            [[KWNNetwork sharedInstance]reportWithQuestionId:_string_id andReportStr:buttontitle FinishBlock:^(NSInteger stateCode, NSMutableArray *result) {
                switch (stateCode) {
                    case 10000:
                    {
                        [SVProgressHUD dismissWithSuccess:@"您的举报信息提交成功" afterDelay:1.5];
                    }
                        break;
                    case 10003:
                    {
                        [SVProgressHUD dismiss];
                        [self messageTokenIsFailure];
                    }
                        break;
                    default:
                    {
                        NSString *string_msg  = result[0];
                        [SVProgressHUD dismissWithError:string_msg afterDelay:1.5];
                    }
                        break;
                }
            }];
        }
        else
        {
            KWNFeedbackViewController *feedbackControl = [[KWNFeedbackViewController alloc] initWithQid:_string_id];
            [self pushViewController:feedbackControl];
        }
    }
}

#pragma mark - UITableViewDelegate -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_arr_messageData.count > 0) {
        Model_questionDetailNew *qD = [_arr_messageData objectAtIndex:0];
        
        if (qD.model_Comments) {
            return _arr_messageData.count + 1;
        }
        else
        {
            return _arr_messageData.count;
        }
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //文字图片cell
    static NSString *ImageAndTextCellIdentifier = @"ImageAndTextCellIdentifier";
    KFWMessageTabCell *imageAndTextCell = (KFWMessageTabCell*)[tableView dequeueReusableCellWithIdentifier:ImageAndTextCellIdentifier];
    //文字cell
    static NSString *TextCellIdentifier = @"TextCellIdentifier";
    KFWMessageTabCell *textCell = (KFWMessageTabCell*)[tableView dequeueReusableCellWithIdentifier:TextCellIdentifier];
    //图片cell
    static NSString *ImageCellIdentifier = @"ImageCellIdentifier";
    KFWMessageTabCell *imageCell = (KFWMessageTabCell*)[tableView dequeueReusableCellWithIdentifier:ImageCellIdentifier];
    //评论cell
    static NSString *CommentCellIdentifier = @"CommentCellIdentifier";
    KFWMessageCommentCell *commentCell = nil;
    
    Model_questionDetailNew *qD = [_arr_messageData objectAtIndex:0];
    NSInteger num_area = qD.string_area.integerValue;
    if (qD.model_Comments) {
        commentCell = (KFWMessageCommentCell *)[tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier];
    }
    
    //第一行问题信息
    if (indexPath.row == 0) {
        if (qD.arr_thumbnail && qD.arr_thumbnail.count > 0) {
            //图片文字都有的问题
            if (imageAndTextCell == nil) {
                imageAndTextCell = [[KFWMessageTabCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ImageAndTextCellIdentifier];
                [imageAndTextCell creatSubViewsWithState:KFWMessageCellStateTextAddImage];
            }
            //文字行
            [imageAndTextCell changeViewFrameWith:qD];
            imageAndTextCell.label_timeLabel.text = qD.string_time;
            imageAndTextCell.imageview_head.image = [UIImage imageFileName:@"head_yonghu.png"];
            imageAndTextCell.label_nameLab.text = [NSString stringWithFormat:@"%@网友",[_array_location objectAtIndex:num_area]];
            imageAndTextCell.label_textLab.text = qD.string_question;
            //图片行
            imageAndTextCell.imageview_imagehead.image = [UIImage imageFileName:@"head_yonghu.png"];
            imageAndTextCell.label_nameImageLab.text = [NSString stringWithFormat:@"%@网友",[_array_location objectAtIndex:num_area]];
            [imageAndTextCell.imageview_one sd_setImageWithURL:[NSURL URLWithString:qD.arr_thumbnail[0]] placeholderImage:[UIImage imageFileName:Default_portrait]];
            if (qD.arr_thumbnail.count > 1) {
                [imageAndTextCell.imageview_two sd_setImageWithURL:[NSURL URLWithString:qD.arr_thumbnail[1]] placeholderImage:[UIImage imageFileName:Default_portrait]];
            }
            else
            {
                imageAndTextCell.imageview_two.image = nil;
            }
            imageAndTextCell.indexPath = indexPath;
            imageAndTextCell.delegate = self;
            return imageAndTextCell;
        }
        else
        {
            //不含图片的问题
            if (textCell == nil) {
                textCell = [[KFWMessageTabCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TextCellIdentifier];
                [textCell creatSubViewsWithState:KFWMessageCellStateText];
            }
            
            [textCell changeViewFrameWith:qD];
            
            textCell.label_timeLabel.text = qD.string_time;
            textCell.imageview_head.image = [UIImage imageFileName:@"head_yonghu.png"];
            NSInteger num_area = qD.string_area.integerValue;
            textCell.label_nameLab.text = [NSString stringWithFormat:@"%@网友",[_array_location objectAtIndex:num_area]];
            textCell.label_textLab.text = qD.string_question;

            textCell.indexPath = indexPath;
            textCell.delegate = self;
            return textCell;
        }
    }
    else if (indexPath.row == _arr_messageData.count)
    {
        //有评论的最后一行
        if(commentCell == nil)
        {
            commentCell = [[KFWMessageCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CommentCellIdentifier];
        }
        
        Model_QuestionComments *comment = qD.model_Comments;
        [commentCell frameOfSubViewWithData:comment];
        commentCell.label_timeLabel.text = comment.string_time;
        
        if ([comment.string_adopt integerValue] == 0) {
            //用户采纳
            commentCell.imageview_headOne.image = [UIImage imageFileName:@"head_yonghu.png"];
            commentCell.label_nameCommentLab.text = [NSString stringWithFormat:@"%@网友",[_array_location objectAtIndex:num_area]];
            
            commentCell.imageview_headTwo.image = [UIImage imageFileName:@"head_yonghu.png"];
            commentCell.label_nameLab.text = [NSString stringWithFormat:@"%@网友",[_array_location objectAtIndex:num_area]];
            commentCell.imageview_medal.image = [UIImage imageFileName:@"pingjia_yonghu.png"];

        }
        else
        {
            //系统采纳
            commentCell.imageview_headOne.image = [UIImage imageFileName:@"head_comment.png"];
            commentCell.label_nameCommentLab.text = [NSString stringWithFormat:@"来问\n小助手"];
            
            commentCell.imageview_headTwo.image = [UIImage imageFileName:@"head_comment.png"];
            commentCell.label_nameLab.text = [NSString stringWithFormat:@"来问\n小助手"];
            commentCell.imageview_medal.image = [UIImage imageFileName:@"pingjia_xitong.png"];
        }
        
        [commentCell.tq_xiangying setScore:comment.string_xy.floatValue/5.0 withAnimation:NO];
        [commentCell.tq_fuwu setScore:comment.string_fuwu.floatValue/5.0 withAnimation:NO];
        [commentCell.tq_zhuanye setScore:comment.string_zy.floatValue/5.0 withAnimation:NO];
        if (comment.string_content && comment.string_content.length >0) {
            commentCell.label_textLab.text = comment.string_content;
        }
        
        return commentCell;
    }
    else
    {
        //除问题行和评论行
        Model_message *message = _arr_messageData[indexPath.row];
        
        //聊天内容存在
        if (message.string_content && message.string_content.length > 0 ) {
            
            //图片存在
            if (message.arr_thumbnail && message.arr_thumbnail.count > 0) {
                
                if (imageAndTextCell == nil) {
                    imageAndTextCell = [[KFWMessageTabCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ImageAndTextCellIdentifier];
                    [imageAndTextCell creatSubViewsWithState:KFWMessageCellStateTextAddImage];
                }
                [imageAndTextCell changeViewFrameWith:message];
                //文字行
                imageAndTextCell.label_timeLabel.text = message.string_time;
                if (message.string_type.integerValue == 0) {
                    [imageAndTextCell.imageview_head sd_setImageWithURL:[NSURL URLWithString:message.string_headUrl] placeholderImage:[UIImage imageFileName:Default_portrait]];
                    imageAndTextCell.label_nameLab.text = [NSString stringWithFormat:@"%@律师",message.string_name];
                }
                else
                {
                    imageAndTextCell.imageview_head.image = [UIImage imageFileName:@"head_yonghu.png"];
                    imageAndTextCell.label_nameLab.text = [NSString stringWithFormat:@"%@网友",[_array_location objectAtIndex:num_area]];
                }
                imageAndTextCell.label_textLab.text = message.string_content;
                
                //图片行
                if (message.string_type.integerValue == 0) {
                    [imageAndTextCell.imageview_imagehead sd_setImageWithURL:[NSURL URLWithString:message.string_headUrl] placeholderImage:[UIImage imageFileName:Default_portrait]];
                    imageAndTextCell.label_nameImageLab.text = [NSString stringWithFormat:@"%@律师",message.string_name];

                }
                else
                {
                    imageAndTextCell.imageview_imagehead.image = [UIImage imageFileName:@"head_yonghu.png"];
                    imageAndTextCell.label_nameImageLab.text = [NSString stringWithFormat:@"%@网友",[_array_location objectAtIndex:num_area]];
                }
                
                [imageAndTextCell.imageview_one sd_setImageWithURL:[NSURL URLWithString:message.arr_thumbnail[0]] placeholderImage:[UIImage imageFileName:Default_portrait]];
                if (message.arr_thumbnail.count > 1) {
                    [imageAndTextCell.imageview_two sd_setImageWithURL:[NSURL URLWithString:message.arr_thumbnail[1]] placeholderImage:[UIImage imageFileName:Default_portrait]];
                }
                else
                {
                    imageAndTextCell.imageview_two.image = nil;
                }
                imageAndTextCell.indexPath = indexPath;
                imageAndTextCell.delegate = self;
                return imageAndTextCell;
            }
            else   //图片不存在
            {
                if (textCell == nil) {
                    textCell = [[KFWMessageTabCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TextCellIdentifier];
                    [textCell creatSubViewsWithState:KFWMessageCellStateText];
                }
                
                [textCell changeViewFrameWith:message];
                textCell.label_timeLabel.text = message.string_time;
                
                if (message.string_type.integerValue == 0) {
                    [textCell.imageview_head sd_setImageWithURL:[NSURL URLWithString:message.string_headUrl] placeholderImage:[UIImage imageFileName:Default_portrait]];
                    textCell.label_nameLab.text = [NSString stringWithFormat:@"%@律师",message.string_name];

                }
                else
                {
                    textCell.imageview_head.image = [UIImage imageFileName:@"head_yonghu.png"];
                    textCell.label_nameLab.text = [NSString stringWithFormat:@"%@网友",[_array_location objectAtIndex:num_area]];
                }
                textCell.label_textLab.text = message.string_content;

                textCell.indexPath = indexPath;
                textCell.delegate = self;
                return textCell;
            }
            
        }
        else  //没有聊天内容
        {
            if (imageCell == nil) {
                imageCell = [[KFWMessageTabCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ImageAndTextCellIdentifier];
                [imageCell creatSubViewsWithState:KFWMessageCellStateImage];
            }
            [imageCell changeViewFrameWith:message];
            imageCell.label_timeLabel.text = message.string_time;

            //图片行
            if (message.string_type.integerValue == 0) {
                [imageCell.imageview_imagehead sd_setImageWithURL:[NSURL URLWithString:message.string_headUrl] placeholderImage:[UIImage imageFileName:Default_portrait]];
                imageCell.label_nameImageLab.text = [NSString stringWithFormat:@"%@律师",message.string_name];

            }
            else
            {
                imageCell.imageview_imagehead.image = [UIImage imageFileName:@"head_yonghu.png"];
                imageCell.label_nameImageLab.text = [NSString stringWithFormat:@"%@网友",[_array_location objectAtIndex:num_area]];
            }
            
            [imageCell.imageview_one sd_setImageWithURL:[NSURL URLWithString:message.arr_thumbnail[0]] placeholderImage:[UIImage imageFileName:Default_portrait]];
            if (message.arr_thumbnail.count > 1) {
                [imageCell.imageview_two sd_setImageWithURL:[NSURL URLWithString:message.arr_thumbnail[1]] placeholderImage:[UIImage imageFileName:Default_portrait]];
            }
            else
            {
                imageCell.imageview_two.image = nil;
            }
            
            imageCell.indexPath = indexPath;
            imageCell.delegate = self;
            return imageCell;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        Model_questionDetailNew *qD = [_arr_messageData objectAtIndex:0];
        return qD.qDHeight;
    }
    else if(indexPath.row == _arr_messageData.count)
    {
        Model_questionDetailNew *qD = [_arr_messageData objectAtIndex:0];
        Model_QuestionComments *comment = qD.model_Comments;
        return comment.qCHeight;
    }
    else
    {
        Model_message *message = [_arr_messageData objectAtIndex:indexPath.row];
        return message.qMHeight;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
#pragma mark - 账号被替换下线 -
/**
 *  账号被替换下线
 */
- (void)messageTokenIsFailure{
    
    UIAlertView *alter_login;
    if(IOS8_VERSION){
        alter_login = [[UIAlertView alloc] initWithTitle:@"您的来问律师账号已在其它地方登录，请您重新登录此设备。如果不是您的操作，您的密码可能已泄露，请及时更改密码，或联系快问律师客服邮箱laiwenlvshi@kuaifawu.com。" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    }
    else{
        alter_login = [[UIAlertView alloc] initWithTitle:nil message:@"您的来问律师账号已在其它地方登录，请您重新登录此设备。如果不是您的操作，您的密码可能已泄露，请及时更改密码，或联系快问律师客服邮箱laiwenlvshi@kuaifawu.com。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    }
    alter_login.tag = 10003;
    [alter_login show];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (alertView.tag) {
        case 10003:{
            KWLLoginViewController *login = [[KWLLoginViewController alloc] initWithTag:6];
            [self pushViewController:login];
        }break;
            
        default:
            break;
    }
}

#pragma mark - KFWMessageTabCellDelegate -

- (void)clickPhoto:(NSInteger)photoTag cellIndexPath:(NSIndexPath *)index
{
    KWNPhotoDetailViewController *detail = [[KWNPhotoDetailViewController alloc] initWithDelegate:self];
    detail.int_tag = 1;
    [_array_photo removeAllObjects];
    
    if (index.row == 0) {
        Model_questionDetailNew *qD = [_arr_messageData objectAtIndex:0];
        
        if (qD.arr_img) {
            [_array_photo addObjectsFromArray:qD.arr_img];
        }
    }
    else
    {
        Model_message *message = [_arr_messageData objectAtIndex:index.row];
        if (message.arr_img) {
            [_array_photo addObjectsFromArray:message.arr_img];
        }
    }
    
    if (photoTag == TagOfView + 2) {
        NSArray *arr = [[_array_photo reverseObjectEnumerator] allObjects];
        [_array_photo removeAllObjects];
        [_array_photo addObjectsFromArray:arr];
    }
    
    [self presentViewController:detail animated:YES completion:^{
    }];
}

- (void)clickHeadCellIndexPath:(NSIndexPath *)index
{
    if (index.row != 0 && index.row != _arr_messageData.count + 1 ) {
        [MobClick event:@"qdetail_portrait"];
        Model_message *message = [_arr_messageData objectAtIndex:index.row];
        NSString *string_layer_id = message.string_id;
        KWLLayerViewController *controller_layer = [[KWLLayerViewController alloc] initWithLaywerWithID:string_layer_id];
        [self pushViewController:controller_layer];
    }
}

#pragma mark - 点击游览图片 -
/**
 *  点击游览图片
 */
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser{
    return _array_photo.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
    MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:_array_photo[index]]];
    return photo;
}

- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index{
    switch (_array_photo.count) {
        case 1:{
            [(KWNPhotoDetailViewController *)photoBrowser scrollPageViewToIndex:index andBoolScroll:NO];
        }break;
        case 2:{
            [(KWNPhotoDetailViewController *)photoBrowser scrollPageViewToIndex:index andBoolScroll:YES];
        }break;
        default:
            break;
    }
}

#pragma mark - KFWMessageInputViewDelegate -
- (void)inputHeightChange:(KFWMessageInputView *)inputView height:(CGFloat)aheight
{
    [UIView animateWithDuration:0.2 animations:^{
//        if (IOS6_VERSION) {
//            [_tab_messageTab setFrameTop:44 - aheight + HeightInputTop];
//        }
//        else
//        {
//            [_tab_messageTab setFrameTop:44 + IOS7_SIZE - 20 - aheight + HeightInputTop];
//        }
        [_tab_messageTab setFrameHeight:MainScreenHeight - _tab_messageTab.frame.origin.y - aheight];
        
    }];
}

- (void)sendText:(KFWMessageInputView *)anMessageInputView
{
    
    [SVProgressHUD showWithStatus:@"发送中..."];
    KFLLaywerTalkRequest *request = [[KFLLaywerTalkRequest alloc] initRequest];
    request.laywerTalk_qId = _string_id;
    request.laywerTalk_content = anMessageInputView.messageTextView.text;
    
    [request startToRequestWithBlock:^(NSInteger stateCode, NSMutableArray *result) {
        
        switch (stateCode) {
            case 10000:
            {
                [SVProgressHUD dismiss];
                anMessageInputView.messageTextView.text = @"";
                
                [_arr_messageData removeAllObjects];
                [_arr_messageData addObjectsFromArray:result];
                
                [_tab_messageTab reloadData];
                
                
                if (_inputView == nil) {
                    if (_arr_messageData.count>0) {
                        Model_questionDetailNew *qD = [_arr_messageData objectAtIndex:0];
                        if ([qD.string_islawyansw integerValue] != 1 && qD.model_Comments == nil) {
                            //可以回答的问题
                            _inputView = [KFWMessageInputView creatKFWMessageInputViewWithFrame:self.view.bounds delegate:self];
                        }
                    }
                }
                else
                {
                    _inputView.messageTextView.text = nil;
                    [_inputView viewResignFirstResponder];
                }
                
                Model_questionDetailNew *qD = [_arr_messageData objectAtIndex:0];
                NSIndexPath *ip = nil;
                
                if (qD.model_Comments) {
                    ip = [NSIndexPath indexPathForRow:_arr_messageData.count inSection:0];
                }
                else
                {
                    ip = [NSIndexPath indexPathForRow:_arr_messageData.count - 1 inSection:0];
                    
                }
                [_tab_messageTab scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:NO];

            }
                break;
            case 10003:
            {
                [SVProgressHUD dismiss];
                [self messageTokenIsFailure];
            }
                break;
            case 10013:
            {
                NSString *msg = result[0];
                [SVProgressHUD dismissWithError:msg afterDelay:1.5];
                
                double delayInSeconds = 1.5;
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    [self didClickButton_back:nil];
                });
            }
                break;
            default:
            {
                NSString *msg = result[0];
                [SVProgressHUD dismissWithError:msg afterDelay:1.5];
            }
                break;
        }
        
        
        
    }];
}

- (void)sendImage:(KFWMessageInputView *)anMessageInputView{}
- (void)sendVoice:(KFWMessageInputView *)anMessageInputView{}

@end
